package com.ws.webpacket.push;

import android.content.Context;
import android.content.Intent;

import com.ws.webpacket.BuildConfig;
import com.ws.webpacket.util.HLog;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

/**
 * 自定义JPush message 接收器, 包括操作tag/alias的结果返回(仅仅包含tag/alias新接口部分)
 * */
public class HReceiveResult extends JPushMessageReceiver {

    // 注册成功回调
    @Override
    public void onRegister (Context context, String s) {
        super.onRegister(context, s);
//        HLog.I("HReceiveResult  onRegister");
    }
    // 注册失败回调
    @Override
    public void onCommandResult (Context context, CmdMessage cmdMessage) {
        super.onCommandResult(context, cmdMessage);
//        HLog.I("HReceiveResult  onCommandResult");
    }

    // 收到自定义消息回调
    @Override
    public void onMessage (Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
//        HLog.I("HReceiveResult onMessage");
    }
    // 收到通知回调
    @Override
    public void onNotifyMessageArrived (Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageArrived(context, notificationMessage);
//        HLog.I("HReceiveResult onNotifyMessageArrived");

    }
    // 点击通知回调
    @Override
    public void onNotifyMessageOpened (Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageOpened(context, notificationMessage);
//        HLog.I("HReceiveResult  onNotifyMessageOpened ");
    }
    // 清除通知回调
    @Override
    public void onNotifyMessageDismiss (Context context, NotificationMessage notificationMessage) {
        super.onNotifyMessageDismiss(context, notificationMessage);
//        HLog.I("HReceiveResult onNotifyMessageDismiss  ");
    }

    //长连接状态回调
    @Override
    public void onConnected (Context context, boolean b) {
        super.onConnected(context, b);
//        HLog.I(" HReceiveResult  onConnected ");

    }

    // 通知的MultiAction回调
    @Override
    public void onMultiActionClicked (Context context, Intent intent) {
        super.onMultiActionClicked(context, intent);
//        HLog.I(" HReceiveResult onMultiActionClicked ");
    }


    //==================================================================================

    // tag 增删查改的操作会在此方法中回调结果。
    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        showJPushMessageInfo("onTagOperatorResult",jPushMessage);
        super.onTagOperatorResult(context, jPushMessage);
    }

    // 查询某个 tag 与当前用户的绑定状态的操作会在此方法中回调结果。
    @Override
    public void onCheckTagOperatorResult(Context context,JPushMessage jPushMessage){
        showJPushMessageInfo("onCheckTagOperatorResult",jPushMessage);
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    // alias 相关的操作会在此方法中回调结果。
    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        showJPushMessageInfo("onAliasOperatorResult",jPushMessage);
        super.onAliasOperatorResult(context, jPushMessage);
    }

    // 设置手机号码会在此方法中回调结果。
    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        showJPushMessageInfo("onMobileNumberOperatorResult",jPushMessage);
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    private void showJPushMessageInfo(String method,JPushMessage jPushMessage){
        if(!BuildConfig.DEBUG)return;
        String result = "";
        result+= "ErrorCode="+jPushMessage.getErrorCode()+"\n";
        result+= "Alias="+jPushMessage.getAlias()+"\n";
        int i = 0;
        for(String item:jPushMessage.getTags()){
            result+= "item"+i+"="+jPushMessage.getAlias()+"\n";
            i++;
        }
        result+= "Sequence="+jPushMessage.getSequence()+"\n";
        result+= "TagCheckStateResult="+jPushMessage.getTagCheckStateResult()+"\n";
        result+= "CheckTag="+jPushMessage.getCheckTag()+"\n";
        result+= "MobileNumber="+jPushMessage.getMobileNumber()+"\n";
        result+= "MobileNumber="+jPushMessage.getMobileNumber()+"\n";

        HLog.I(result);
    }

}
